// pages/live/pages/room/room.js
import utils from '../../../../utils/util'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageUrl: '',
    imageData: '',
    roomDesc: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getLastLive();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  //更换头像
  changeHeadImg: function () {
    let self = this;
    wx.showActionSheet({
      itemList: ['拍照', '从手机相册选择'],
      success(res) {
        console.log(res.tapIndex)
        if (res.tapIndex == 0) { //'拍照'
          self.chooseImage('camera');
        } else if (res.tapIndex == 1) { //从手机相册选择
          self.chooseImage('album');
        }
      },
      fail(res) {
        console.log(res.errMsg);
      }
    })

  },
  //选择图片
  chooseImage: function (flag) {
    let self = this;
    let flagSouse = flag;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: [flagSouse],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths;
        self.upImgFile(tempFilePaths);
      }
    })
  },
  //图片上传
  upImgFile: function (imgUrl) {
    let self = this;
    wx.uploadFile({
      url: app.globalData.apiRootUrl + '/Common/FileUpload',
      filePath: imgUrl[0],
      name: 'file',
      formData: {},
      success: function (res) {
        let rdata = JSON.parse(res.data);
        if (rdata.status == 0) {
          self.setData({
            imageUrl: imgUrl[0],
            imageData: rdata.data
          })
        } else {
          wx.showToast({
            title: rdata.message,
            icon: 'none'
          })
        }
      },
      fail: function (e) {
        wx.showToast({
          title: '上传图片失败',
          icon: 'none'
        })
      }
    })
  },
  /**
   * 表单提交
   */
  formSubmit: function (e) {
    let self = this;
    let data = e.detail.value;
    data.UserHead = self.data.UserHead;
    data.UserID = app.globalData.userInfo.UserID;
    data.Name = app.globalData.userInfo.UserName;
    data.OpenID = app.globalData.userInfo.OpenID;
    data.CoverImage = self.data.imageData;
    data.WeChartUserHead = app.globalData.userInfo.UserHead;
    data.CreatedUserID = data.UserID;
    if (utils.isNullOrEmpty(data.Description)) {
      wx.showToast({
        title: '请输入直播简介',
        icon: "none"
      });
      return;
    }

    let url = app.globalData.apiRootUrl + "/Live/AddLiveRoom";
    app.httpRequest({
      url: url,
      method: "POST",
      data: data,
      success: function (res) {
        let rdata = res.data;
        if (rdata.status == 0) {
          self.gotoPusher(rdata.data);
        } else {
          wx.showToast({
            title: rdata.data.message,
            icon: "none"
          })
        }
      }
    })
  },
  /**
   * 调整到直播室
   * @param {*} params 
   */
  gotoPusher: function (data) {
    var roomName = data.Description
    var anthor = data.Name
    var roomId = data.RoomID;
    var avatar = data.WeChartUserHead;
    var pull_url = data.PusherUrl || '';
    var listString = '?roomName=' + roomName + '&roomId=' + roomId + '&anthor=' + anthor + '&avatar=' + avatar + '&pull_url=' + encodeURIComponent(pull_url);
    wx.navigateTo({
      url: '../pusher/pusher' + listString,
    })
  },
  /**
   * 获取上次直播
   */
  getLastLive: function () {
    let self = this;
    let url = app.globalData.apiRootUrl + "/Live/GetLastLive";
    app.httpRequest({
      url: url,
      method: "GET",
      data: {
        userId: app.globalData.userInfo.UserID
      },
      success: function (res) {
        let rdata = res.data;
        if (rdata.status == 0) {
          self.setData({
            imageUrl: rdata.data.CoverImage,
            imageData: rdata.data.CoverImage,
            roomDesc: rdata.data.Description
          })
        } else {
          wx.showToast({
            title: rdata.data.message,
            icon: "none"
          })
        }
      }
    })
  }
})